package laserdancer.smart.com.criminalintent;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import java.util.UUID;

public class CrimeFragment extends Fragment {

    public static final String EXTRA_CRIME_ID = "criminalintent.CRIME_ID";

    Crime mCrime;
    EditText mEditText;
    Button mDateButton;
    CheckBox mCheckBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCrime = new Crime();

        Bundle _bundle=getArguments();
        UUID crimeId = (UUID)_bundle.getSerializable(EXTRA_CRIME_ID);
        mCrime = CrimeLab.get(getContext()).getCrime(crimeId);
    }

    public static CrimeFragment newInstance(UUID _uuid){
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_CRIME_ID, _uuid);

        CrimeFragment fragment = new CrimeFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,
                         Bundle savedInstanceState){

        View v = inflater.inflate(R.layout.fragment_crime,container,false);

        mEditText = v.findViewById(R.id.crime_title);
        mDateButton = v.findViewById(R.id.crime_date);
        mCheckBox = v.findViewById(R.id.crime_solved);

        mDateButton.setText(mCrime.getDate().toString());
        mDateButton.setEnabled(false);

        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCrime.setSolved(isChecked);
            }
        });

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mCrime.setTitle(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });



        mEditText.setText(mCrime.getTitle());
        mDateButton.setText(mCrime.getDate().toString());
        mCheckBox.setChecked(mCrime.isSolved());

        return v;
    }
}
